package com.sx.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sx.util.Pagination;
import com.sx.bean.Resource;
import com.sx.service.ResourceService;

@Controller
@RequestMapping("res")
public class ResourceController {
	@Autowired
	private ResourceService resourceService;
	@PostMapping("insert.do")
	@ResponseBody
	public Integer addResource(Resource resource) {
		try {
			resourceService.addResource(resource);
		}catch(Exception e) {
			e.printStackTrace();
			return -1;
		}
		return 1;
	}
	@GetMapping("getResources.do")
	@ResponseBody
	public Pagination getResource(Integer pageSize, Integer pageNumber) {
		Pagination resourcePage = null;
		try {
			resourcePage = resourceService.getResourcePage(pageSize, pageNumber);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return resourcePage;
	}
	@GetMapping("getResource.do")
	@ResponseBody
	public Resource getResource(String uuid) {
		Resource resource = null;
		try {
			resource = resourceService.getResource(uuid);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return resource;
	}
	
	@PostMapping("update.do")
	@ResponseBody
	public Integer updateResource(Resource resource) {
		try {
			resourceService.updateResource(resource);
		}catch(Exception e) {
			e.printStackTrace();
			return -1;
		}
		return 1;
	}
	
	
	@GetMapping("getResourcesByParentId.do")
	@ResponseBody
	public List<Resource> getResourceByParentId(String parentId){
		return null;
	}
	
	@GetMapping("remove.do")
	@ResponseBody
	public int remove(String id) {
		try {
			resourceService.remove(id);
		}catch(Exception e) {
			e.printStackTrace();
			return -1;
		}
		return 1;
	}
}
